package com.example.startscreen;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Point;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator.IndicatorStyle;

public class MainActivity extends Activity {

	public static enum languageEnum {
		MONTENEGRO(1), ENGLISH(2);

		private int value;

		private languageEnum(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
	}

	public static int language = languageEnum.MONTENEGRO.getValue();
	// public static int language = languageEnum.ENGLISH.getValue();

	final Context context = this;

	private boolean runOnlyOnce = true;
	private View mainView;

	public static TitlePageIndicator indicatorMain;
	public ViewPagerAdapter mainAdapter;

	public void playNotificationSound() {

		final MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.click3);
		mediaPlayer.start(); // no need to call prepare(); create() does that for you

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		if (runOnlyOnce) {
			runOnlyOnce = false;
			Locale locale = Locale.getDefault();
			String lang = locale.getCountry();
			Log.i("Country: ", "" + lang);
			if (lang != "me") {
				language = languageEnum.ENGLISH.getValue();
				Log.i("OnCreate: ", "Jezik: " + language);
				// dodato radi ekrana
				requestWindowFeature(Window.FEATURE_NO_TITLE);
				getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
			}
		}

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		
		// provjeravanje velicine ekrana i stavljanje odgovarajuceg gifa u zavisnosti od velicine...
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int height = metrics.heightPixels;
		int wwidth = metrics.widthPixels;
		
		
		
		
		// postavljanje viewpagera.. kako bi bilo omoguceno da razliciti layouti
		// zauzimaju ovaj kontenjer
		ViewPagerAdapter adapterView = new ViewPagerAdapter(this, this);

		ViewPager pager = (ViewPager) findViewById(R.id.viewpager);

		// indikator stranice
		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		indicator.setFadingEdgeLength(23);
		indicator.setBackgroundColor(Color.TRANSPARENT); // RED
		indicator.setTextColor(Color.BLACK);
		indicator.setFooterColor(Color.RED); // YELLOW
		indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);
		indicator.setMinimumHeight(24);
		// proba VRATITITITITITITITITITITITITIIIIIIIIIIIIIIIIIIIIIIIIIIIII
		indicator.setBackgroundResource(R.drawable.grey);

		// povezivanje gore navedenih objekata
		pager.setAdapter(adapterView);
		indicator.setViewPager(pager);
		pager.setCurrentItem(0);

		// kreiranje animacije.. zastave Crne Gore..

		// gv.setGIFResource(R.drawable.flagmain);

		// podesavanje pozadine za LAYOUTE
		RelativeLayout mainRelative = (RelativeLayout) findViewById(R.id.RelativeLayout1);
		// main.setBackgroundColor(Color.BLACK);
		LinearLayout mainLayout = (LinearLayout) findViewById(R.id.linearMain);

		// podesavanje glavnog indikatora
		//		this.indicatorMain = indicator;
		//
		//		if (adapterView.page == 2) {
		//			this.indicatorMain.setBackgroundResource(R.drawable.headeryellow4);
		//		}
		//		if (adapterView.page == 3) {
		//			this.indicatorMain.setBackgroundResource(R.drawable.headerbrown);
		//		}

		// zbog lokalizacije
		this.mainAdapter = adapterView;

		// podesavanje boje texta u MAIN LAYoutu
		//		TextView textHeader = (TextView) findViewById(R.id.textView1);
		//		textHeader.setTextColor(Color.BLACK);
		
		// podesavanje stilova u pageru
		pager.setFadingEdgeLength(20);

	}

	// LOCALIZATION
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		// return super.onCreateOptionsMenu(menu);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		// return super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.lang_mne:
			this.setLocalization("mne");
			return true;

		case R.id.lang_eng:
			this.setLocalization("eng");
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		menu.clear();
		MenuInflater inflater = getMenuInflater(); // -->onCreateMenu (Menu)
		inflater.inflate(R.menu.main_menu, menu); // /
		return super.onMenuOpened(featureId, menu);
	}

	public void setLocalization(String lang) {

		if (lang == "mne") {
			Log.i("SetLocalization: ", "mne");
			if (language != languageEnum.MONTENEGRO.getValue()) {
				language = languageEnum.MONTENEGRO.getValue();
				Locale locale = new Locale("me");
				Locale.setDefault(locale);
				Configuration config = new Configuration();
				config.locale = locale;
				getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
				Log.i("Forsiramo country: ", "Montenegro");
				setContentView(R.layout.main);
				onCreate(null);
				this.setTitle(this.getResources().getText(R.string.app_name));

				//zamjena i cuvanje engleskih naslova LOKALIZACIJA VIEW PAGER LAYOUT-A
				this.mainAdapter.most = this.mainAdapter.titles;
				this.mainAdapter.titles = this.mainAdapter.naslovi;

			}
		} else if (lang == "eng") {
			Log.i("SetLocalization: ", "eng");
			if (language != languageEnum.ENGLISH.getValue()) {
				language = languageEnum.ENGLISH.getValue();
				Locale locale = new Locale("en");
				Locale.setDefault(locale);
				Configuration config = new Configuration();
				config.locale = locale;
				getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
				Log.i("Forsiramo country: ", "England");
				setContentView(R.layout.main);
				onCreate(null);
				this.setTitle(this.getResources().getText(R.string.app_name));

				//zamjena i cuvanje engleskih naslova LOKALIZACIJA VIEW PAGER LAYOUT-A
				this.mainAdapter.titles = this.mainAdapter.most;

			}
		}

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		setContentView(R.layout.main);
		// this.setTitle(this.getResources().getText(R.string.app_name));
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

}
